#Region " Imports "

Imports BlazeApps.Library.Resources
Imports System.Data
Imports BlazeApps.Library.Options

#End Region

Partial Class Usercontrols_Gallery_Galleries
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Public Property MyGalleries() As Boolean
        Get
            Return GetBooleanFromVS("MyGalleries")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("MyGalleries", value)
        End Set
    End Property

    Public Property GalleriesByUser() As Boolean
        Get
            Return GetBooleanFromVS("GalleriesByUser")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("GalleriesByUser", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Galleries")

        If Me.Page.IsPostBack = False Then

            'Set Default Sort Order and Direction
            Me.sortCriteria = "createdate"
            Me.sortDir = "desc"

            LoadPage()
        End If

        ConfigBaseNavCtrls()

        If Me.Page.IsPostBack = False Then
            BindDataItems()
        End If

        Me.uxAddContentPanel.Visible = IsAllowed()

    End Sub

#End Region

#Region " Save Data "


#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()
        Dim List As New ResourceList
        Dim DS As New DataSet
        Dim DT As New DataTable
        Dim DV As New DataView

        If GetBooleanFromQueryString("MyGalleries") OrElse MyGalleries = True Then
            If IsUserAuthenticated Then
                DS = List.GetResourceListByUser(CurrentUserID)
                Me.uxTitleLiteral.Text = String.Format("{0} Gallery", CurrentUser)
            Else
                DS = List.GetResourceList()
            End If
        ElseIf GetBooleanFromQueryString("UsersGalleries") OrElse GalleriesByUser Then
            DS = List.GetResourceListByUser(CurrentUserID(GetStringFromQueryString("userid")))
            Me.uxTitleLiteral.Text = String.Format("{0} Gallery", GetStringFromQueryString("userid"))
        Else
            DS = List.GetResourceList()
        End If

        DT = DS.Tables(0)

        DV.Table = DT

        DV.RowFilter = String.Format("contenttype like '%image/%' or contenttype like '%video/%' or contenttype like '%application/octet-stream%' or contenttype like '%application/x-shockwave-flash%'")

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        Me.RecordCount = Me.DataPage.DataSourceCount

        CreatePagingLinks()

        With Me.uxGalleryDataList
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddContentButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddContentButton.Click
		Response.Redirect("~/gallery.aspx?pid=gal_mygallery")
    End Sub

    Protected Sub uxGalleryDataList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles uxGalleryDataList.ItemDataBound
        Dim IB As ImageButton = CType(e.Item.FindControl("uxDeleteItemImageButton"), ImageButton)
        Dim LB As LinkButton = CType(e.Item.FindControl("uxDeleteItemLinkButton"), LinkButton)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            'If ShowItem(ConvertToGuid(e.Item.DataItem("resourceid").ToString)) Then

            If IsUserAuthenticated Then
                If IsAllowedDelete(ConvertToGuid(e.Item.DataItem("userid").ToString().Trim())) Then
                    IB.Visible = True
                    LB.Visible = True
                End If
            End If

        End If
    End Sub

    Protected Sub DeleteItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        ResourceDetail.DeleteResourceDetail(ConvertToGuid(e.CommandArgument.ToString))
        BindDataItems()
    End Sub

    Protected Sub LoadGalleryItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("~/default.aspx?pid=gal_galleryviewer&id={0}", e.CommandArgument.ToString))
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Function IsAllowed() As Boolean
        If GetBooleanFromQueryString("UsersGalleries") OrElse GalleriesByUser Then
            Return False
        Else
            Return (((MyGalleries OrElse IsUserAuthenticated) And EnableUserGalleries) OrElse IsGalleryModerator())
        End If
    End Function

    Private Function IsAllowedEdit(ByVal UserID As String) As Boolean
        Return (CurrentUserID = ConvertToGuid(UserID) OrElse IsGalleryModerator())
    End Function

    Private Function IsAllowedDelete(ByVal UserID As Guid) As Boolean
        Return (CurrentUserID = ConvertToGuid(UserID) OrElse IsGalleryModerator())
    End Function

#End Region

End Class
